package com.etl.dataflow.scheduler.admin.controller;


import com.etl.dataflow.common.response.R;
import com.etl.dataflow.scheduler.admin.entity.DataflowTaskRecord;
import com.etl.dataflow.scheduler.admin.entity.request.TaskRecordPageRequest;
import com.etl.dataflow.scheduler.admin.service.DataflowTaskRecordService;
import com.etl.dataflow.scheduler.admin.validation.Add;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 任务运行记录 前端控制器
 * </p>
 *
 * @author dx
 * @since 2022-04-24
 */
@RestController
@RequestMapping("/admin/task-record")
public class DataflowTaskRecordController implements DefaultController {
    private final DataflowTaskRecordService taskInfoService;

    public DataflowTaskRecordController(DataflowTaskRecordService taskInfoService) {
        this.taskInfoService = taskInfoService;
    }

    @PostMapping("/addOrUpdate")
    public R add(@RequestBody @Validated(Add.class) DataflowTaskRecord datasource) {
        return successOrError(taskInfoService.saveOrUpdate(datasource));
    }

    @GetMapping("/remove")
    public R remove(@RequestParam @NotNull Long id) {
        return successOrError(taskInfoService.removeById(id));
    }

    @GetMapping("/find")
    public R find(@RequestParam @NotEmpty String id) {
        return taskInfoService.findVoById(id);
    }

    @GetMapping("/taskStates")
    public R findAllTaskStates() {
        return taskInfoService.findAllTaskStates();
    }

    @PostMapping("/paging")
    public R paging(@RequestBody @Validated TaskRecordPageRequest pageRequest) {
        return R.success().put("data", taskInfoService.page(pageRequest));
    }

    @GetMapping("/statistic")
    public R statistic(@RequestParam @NotBlank String executorUuid, @RequestParam @NotNull Long taskId) {
        return taskInfoService.statistic(executorUuid, taskId);
    }
}
